//
//  ZYRunloopObserver.m
//  OC_Principle
//
//  Created by wangzhipan on 2025/2/12.
//

#import "ZYRunloopObserver.h"
#import <CoreFoundation/CoreFoundation.h>

@implementation ZYRunloopObserver

- (void)observeMainRunLoop
{
    
    CFRunLoopObserverRef observerRef = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
            {
                NSLog(@"-----进入runloop");
                break;
            }
            case kCFRunLoopBeforeTimers:
            {
                NSLog(@"-----即将处理timers");
                break;
            }
            case kCFRunLoopBeforeSources:
            {
                NSLog(@"-----即将处理Sources");
                break;
            }
            case kCFRunLoopBeforeWaiting:
            {
                NSLog(@"-----即将休眠");
                break;
            }
            case kCFRunLoopAfterWaiting:
            {
                NSLog(@"-----被唤醒");
                break;
            }
            case kCFRunLoopExit:
            {
                NSLog(@"-----退出runloop");
                break;
            }
            default:
                break;
        }
    });
    
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observerRef, kCFRunLoopCommonModes);
    CFRelease(observerRef);
}



@end
